package com.jt.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;

@Service
public class OrderServiceImpl implements DubboOrderService {
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;


	/**
	 * 订单入库操作
	 */
	@Override
	@Transactional
	public String saveOrder(Order order) {
		String orderId=""+order.getUserId()+System.currentTimeMillis();
		//完成订单入库操作
		order.setOrderId(orderId).setStatus(1);//未付款状态
		orderMapper.insert(order);

		//订单物流信息入库
		OrderShipping orderShipping = order.getOrderShipping();
		orderShipping.setOrderId(orderId);
		orderShippingMapper.insert(orderShipping);

		//订单商品入库
		List<OrderItem> orderItems = order.getOrderItems();
		for (OrderItem orderItem : orderItems) {
			orderItem.setOrderId(orderId);
			orderItemMapper.insert(orderItem);
		}
		System.out.println("订单入库完成");
		return orderId;
	}

	/**
	 * 订单页面的查询及展现
	 */
	@Override
	public Order findOrderById(String orderId) {
		//查询订单信息
		Order order = orderMapper.selectById(orderId);
		//查询订单物流信息
		OrderShipping orderShipping = orderShippingMapper.selectById(orderId);
		//查询订单商品信息
		QueryWrapper<OrderItem> wrapper = new QueryWrapper<>();
		wrapper.eq("order_id", orderId);
		List<OrderItem> orderItems = orderItemMapper.selectList(wrapper);

		order.setOrderItems(orderItems);
		order.setOrderShipping(orderShipping);
		return order;
	}
}
